C# wps转pdf(word、ppt、excel),在线预览pdf 您所在的位置:网站首页 wps word转换pdf C# wps转pdf(word、ppt、excel),在线预览pdf

C# wps转pdf(word、ppt、excel),在线预览pdf

2023-09-24 15:21| 来源: 网络整理| 查看: 265

wps转pdf

  注:我是在wps试用期专业版,windows10系统  vs2019 webform(.net framework4.5)测试。

  前提:需要下载安装wps专业版、企业版。

  项目中需要引用wps的com组件

    com组件Upgrade WPS Spreadsheets 3.0 Object Library (Beta)  ,对应“Excel”,C:\WINDOWS\assembly\GAC_32\Kingsoft.Office.Interop.Etapi\3.0.0.0__15d99fb7f8fe5cb4\Kingsoft.Office.Interop.Etapi.dll

    com组件 Upgrade WPS Presentation 3.0 Object Library (Beta),对应“PowerPoint”,C:\WINDOWS\assembly\GAC_32\Kingsoft.Office.Interop.Wppapi\3.0.0.0__15d99fb7f8fe5cb4\Kingsoft.Office.Interop.Wppapi.dll

    com组件Upgrade Kingsoft WPS 3.0 Object Library (Beta),对应“Word”,C:\WINDOWS\assembly\GAC_32\Kingsoft.Office.Interop.Wpsapi\3.0.0.0__15d99fb7f8fe5cb4\Kingsoft.Office.Interop.Wpsapi.dll

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 public  static  class  WpsToPdf {      ///      /// word转pdf      ///      /// 源.      /// 新文件路径.      /// The .      public  static  bool  WordWpsToPdf( string  source,  string  newFilePath)      {          if  (source ==  null )  throw  new  ArgumentNullException(nameof(source));          if  (newFilePath ==  null )  throw  new  ArgumentNullException(nameof(newFilePath));            var  type = Type.GetTypeFromProgID( "KWps.Application" );          dynamic wps = Activator.CreateInstance(type);          try          {              //用wps打开word不显示界面              dynamic doc = wps.Documents.Open(source, Visible:  false );                //转pdf              doc.ExportAsFixedFormat(newFilePath, WdExportFormat.wdExportFormatPDF);                //设置隐藏菜单栏和工具栏              //wps.setViewerPreferences(PdfWriter.HideMenubar | PdfWriter.HideToolbar);              doc.Close();          }          catch  (Exception e)          {              //添加你的日志代码              return  false ;          }          finally          {              wps.Quit();              GC.Collect();              GC.WaitForPendingFinalizers();          }            return  true ;      }        ///      /// excel转pdf      ///      /// 源.      /// 新文件路径.      /// The .      public  static  bool  ExcelToPdf( string  source,  string  newFilePath)      {          if  (source ==  null )  throw  new  ArgumentNullException(nameof(source));          if  (newFilePath ==  null )  throw  new  ArgumentNullException(nameof(newFilePath));            var  type = Type.GetTypeFromProgID( "KET.Application" );          dynamic wps = Activator.CreateInstance(type);          try          {              var  targetType = XlFixedFormatType.xlTypePDF;              var  missing = Type.Missing;              //转pdf              var  doc = wps.Application.Workbooks.Open(source, missing, missing, missing, missing, missing,                  missing, missing, missing, missing, missing, missing, missing, missing, missing);              doc.ExportAsFixedFormat(targetType, newFilePath, XlFixedFormatQuality.xlQualityStandard,  true ,  false ,                  missing, missing, missing, missing);              doc.Close();          }          catch  (Exception e)          {              //添加你的日志代码              return  false ;          }          finally          {              wps.Quit();              GC.Collect();              GC.WaitForPendingFinalizers();          }          return  true ;      }        ///      /// ppt转pdf      ///      /// 源.      /// 新文件路径.      /// The .      public  static  bool  PptToPdf( string  source,  string  newFilePath)      {          var  type = Type.GetTypeFromProgID( "KWPP.Application" );          dynamic wps = Activator.CreateInstance(type);          try          {              //转pdf              var  doc = wps.Presentations.Open(source, MsoTriState.msoCTrue, MsoTriState.msoCTrue,                  MsoTriState.msoCTrue);              doc.SaveAs(newFilePath, PpSaveAsFileType.ppSaveAsPDF, MsoTriState.msoTrue);              doc.Close();          }          catch  (Exception e)          {              //添加你的日志代码              return  false ;          }          finally          {              wps.Quit();              GC.Collect();              GC.WaitForPendingFinalizers();          }          return  true ;      } }


【本文地址】

公司简介

联系我们

今日新闻

    推荐新闻

    专题文章
      CopyRight 2018-2019 实验室设备网 版权所有